package fons.navigator.ui.configurations;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.google.common.collect.Lists;

import fons.util.Pair;

public class CharConfigurationComponentFactoryMethod extends
AbstractConfigurationComponentFactory<Character> {
	JTextField text;

	public CharConfigurationComponentFactoryMethod() {
		super(Character.class);
	}

	@Override
	public JComponent getInput() {
		text = new JTextField("" + getValue());
		text.setColumns(1);
		return text;
	}
	
	@Override
	public JComponent getLabel() {
		JLabel label = (JLabel) super.getLabel();
		label.setText(label.getText() + " (Einzelnes Zeichen)");
		return label;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Pair<JComponent, ActionListener>> getAdditionalComponents() {
		return Lists.newArrayList(Pair.<JComponent, ActionListener>of(
				new JLabel("<html><body><strong>Hinweis:</strong>Wenn Sie mehr als ein Zeichen eingeben,<br>" +
						"wird nur das erste Zeichen verwendet.</body></html>"),
				null
				));
	}

	@Override
	public ActionListener createActionListenerFor(JComponent component) {
		return new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (text.getText().length() > 0) {
					setValue(text.getText().charAt(0));
				}
			}
		};
	}
}
